import java.util.ArrayList;
import java.util.List;

public class Test01 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> ret = new ArrayList<>();
        char[] ss = s.toCharArray();
        char[] pp = p.toCharArray();
        int[] hash1 = new int[26];
        for(int i = 0; i < pp.length ; i++){
            hash1[ pp[i] - 'a']++;
        }
        int[] hash2 = new int[26];
        int m = pp.length;
        for(int left = 0 , right = 0,count = 0; right < ss.length; right++){
            hash2[ss[right] - 'a']++;
            if(hash2[ss[right] - 'a'] <= hash1[ss[right] - 'a']){
                count++;
            }
            if(right - left + 1 > m){
                if (hash2[ss[left] - 'a'] <= hash1[ss[left] - 'a']){
                    count--;
                }
                hash2[ss[left] - 'a']--;
                left++;
            }
            if(count == m){
                ret.add(left);
            }

        }
        return ret;
    }
}
